package demo.factor.Ifactor;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;

import demo.bean.Carts;
import demo.bean.Client;
import demo.bean.Goods;
import demo.factor.PeoFactory.PeoFactory;

public class IFactory {
	
	//用户的  增，删，查
	public static boolean addClient(Client client)
	{
		boolean flag=true;
		Map<Client,Integer> a=PeoFactory.fa.readAllUser();
		client.setId(new Random().nextInt(100));
		Set<Map.Entry<Client,Integer>> en=a.entrySet();
		for (Map.Entry<Client,Integer> key : en) {
			if(client.getName().equals(key.getKey().getName()))
			{
				return false;
			}
		}
		a.put(client, client.getId());
		PeoFactory.fa.writeAllUser(a);
		return flag;
	}
	public static boolean deleteClient(int id)
	{
		Map<Client,Integer> a=PeoFactory.fa.readAllUser();
		System.out.println("ssssssssssss"+a.toString());
		Set<Entry<Client, Integer>> es=a.entrySet();
		for (Object obj1 : es) {
			Map.Entry<Client, Integer> e=(Map.Entry)obj1;
			System.out.println(e.getValue());
			int a1=e.getValue();
			if(a1==id)
			{
				Integer b=a.remove(e.getKey());
				PeoFactory.fa.writeAllUser(a);
				if(b==id)
					return true; 
							
			}
		}
		return false;
	}
	public static Map<Client,Integer> selectAllClient()
	{
		Map<Client,Integer> a=PeoFactory.fa.readAllUser();
		return a;	
	}
	public static Client selectAClient(String name)
	{
		Map<Client,Integer> a=PeoFactory.fa.readAllUser();
		Set<Entry<Client, Integer>> es=a.entrySet();
		for (Object obj1 : es) {
			Map.Entry<Client, Integer> e=(Map.Entry)obj1;
			
			if(name.equals(e.getKey().getName()))
			{
					return e.getKey(); 				
			}
		}
		return null;
		
	}
	
	//商品的  增   删    改   查
	public static boolean addAGoods(Goods good)
	{
		Set<Goods> goods=PeoFactory.fa.readAllGoods();
		good.setGoodsId(new Random().nextInt(1000));
		boolean flag=goods.add(good);
		PeoFactory.fa.writeAllGoods(goods);
		return flag;
		
	}
	public static boolean alertGoods(Goods good)
	{
		deleteGoods(good.getGoodsId());
		return addAGoods(good);	
	}
	public static Set<Goods> selectAllGoods()
	{
		Set<Goods> goods=PeoFactory.fa.readAllGoods();
		return goods;	
	}
	public static Goods selectAGoods(int number)
	{
		Set<Goods> goods=PeoFactory.fa.readAllGoods();
		
		for(Goods g:goods)
		{
			int a=g.getGoodsId();
			
			if(a==number)
			{
				return g;
			}
		}
		return null;	
	}
	public static boolean deleteGoods(int id)
	{
		Set<Goods> goods=PeoFactory.fa.readAllGoods();
		Iterator<Goods> iter=goods.iterator();
	     while(iter.hasNext())
	     {
	    	 Goods g=iter.next();
	    	 int a=g.getGoodsId();
	    	 if(a==id)
	    	 {
	    		 boolean b=goods.remove(g);
	    		 PeoFactory.fa.writeAllGoods(goods);
	    		 return  b;
	    	 }
	    
	     }
		
		return false;
		
	}
	
	//购物车查 找 
	public static Carts selectABuyList(Client client)
	{
		String name=client.getName();
		Map<Client,Carts> cartlist=PeoFactory.fa.readAllCarts();
		Set<Map.Entry<Client,Carts>> set=cartlist.entrySet();
		for(Map.Entry<Client,Carts> c:set)
		{
			if(c.getKey().getName().equals(name))
			{
				return c.getValue();
			}
		}
		System.out.println("找不到这个购物车");
		
		return null;	
	}
	//购物车添加一个
	public static boolean addACarts(Carts car,Client c)
	{
		Map<Client,Carts> map=PeoFactory.fa.readAllCarts();
		map.put(c,car);
		return PeoFactory.fa.writeAllCarts(map);
		
	}
	
	
	
}
